import {Button} from '@/components';
import filter from '@/components/filter';
import { Component } from 'react';

export default class FooterAction extends Component {
  // 是否不可点击
  isDisabled() {
    return this.loading;
  }

  onClick = (e) => {
    const { onClick } = this.props;
    if (!onClick) {
      return;
    }
    let p = onClick(e);
    if (p && p.finally) {
      this.loading = true;
      this.setState({});
      p.finally(() => {
        this.loading = false;
        this.setState({});
      });
    }
  };

  render() {
    const {
      name,
      promiseLock,
      children,
      onClick,
      disabled,
      loading,
      ...props
    } = this.props;

    let clickAction = onClick;

    let dis = filter.parse(disabled);
    // promise 按钮锁定
    if (promiseLock) {
      clickAction = this.onClick;
      dis = dis || this.isDisabled();
    }

    return (
      <Button
        onClick={clickAction}
        loading={loading || this.loading}
        disabled={dis}
        {...props}
      >
        {children}
      </Button>
    );
  }
}
